package com.example.gray.controller;

import com.example.gray.entity.*;
import com.example.gray.service.OpLogService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Gray 2021/12/28-18:28
 */
@RestController
@CrossOrigin
@Api(tags = "日志")
@RequestMapping(value = "log")
public class LogController {

    @Autowired
    private OpLogService logService;

    @GetMapping(value = "select/all")
    @ApiOperation(value = "查询所有日志")
    public Object selectAll(Integer page,Integer size){
        PageInfo<OpLog> result = logService.selectAll(page,size);
        return Result.success(result);
    }

    @GetMapping(value = "select/userId")
    @ApiOperation(value = "查询特定用户日志")
    public Object selectById(Integer id){
        return Result.success(logService.selectById(id));
    }

    @GetMapping(value = "select/ip")
    @ApiOperation(value = "查询特定ip日志")
    public Object selectByIp(String ip){
        return Result.success(logService.selectByIp(ip));
    }

    @GetMapping(value = "select/time")
    @ApiOperation(value = "查询时间区域日志")
    public Object selectByTime(String S,String E){
        return Result.success(logService.selectByTime(S,E));
    }

    @GetMapping(value = "search/dailyVisits")
    @ApiOperation(value = "查询每日访问量/周")
    public Object groupByDate(String startTime,String endTime){
        return Result.success(logService.groupByDate(startTime,endTime));
    }

    @GetMapping(value = "search/ipTop10")
    @ApiOperation(value = "查询时间段内访问量前十的ip地址")
    public Object searchTop10(String startTime,String endTime){
        List<LogIp> logs = logService.getTop10(startTime, endTime);
        List<LogIp> responseLog = new ArrayList<>();
        int count = 0;
        for (LogIp log :
                logs) {
            responseLog.add(log);
            count++;
            if (count == 10) {
                break;
            }
        }
        return Result.success(responseLog);
    }

    @GetMapping(value = "search/timeZone")
    @ApiOperation(value = "查询24h内各区间访问量")
    public Object searchTimeZone(){
        List<TimeAccess> list = logService.getTimeZone();
        int[] result = new int[4];
        for (TimeAccess t :
                list) {
            if (t.getHour()<9) {
                result[0]+=t.getCount();
            }else if (t.getHour()<14){
                result[1]+=t.getCount();
            }else if (t.getHour()<20){
                result[2]+=t.getCount();
            }else if (t.getHour()<24){
                result[3]+=t.getCount();
            }
        }
        return Result.success(result);
    }

    @PostMapping(value = "insert")
    @ApiOperation(value = "插入数据")
    public Object insert(@RequestBody LogList logList){
        try {
            int count=0;
            for (OpLog log :
                    logList.getLogList()) {
                logService.insert(log);
                count++;
            }
            return Result.success("成功插入"+count+"条数据");
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(4001,"插入失败","");
        }

    }

    @PostMapping(value = "delete")
    @ApiOperation(value = "删除数据")
    public Object insert(@RequestBody IdList idList){
        System.out.println(idList);
        int count=0;
        try {
            for (Integer id:
                    idList.getIdList()) {
                logService.delete(id);
                count++;
            }
            return Result.success("成功删除"+count+"条数据");
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(4001,"删除失败","");
        }
    }

    @PostMapping(value = "update")
    @ApiOperation(value = "更新数据")
    public Object update(@RequestBody LogList logList){
        try {
            int count=0;
            for (OpLog log :
                    logList.getLogList()) {
                logService.update(log);
                count++;
            }
            return Result.success("成功更新"+count+"条数据");
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(4001,"更新失败","");
        }
    }
}
